In my previous post “Translate from pheatmap to ComplexHeatmap”, I introduced a ComplexHeatmap::pheatmap() which smoothly executes pheatmap::pheatmap() code without any problem. With the newly developed InteractiveComplexHeatmap package, the output of pheatmap() can be directly exported as an interactive Shiny app.
library(ComplexHeatmap)
ht = pheatmap(...)
library(InteractiveComplexHeatmap)
ht_shiny(ht)
To facilitate the users who are still using heatmap() and heatmap.2() functions, to make the output of these two functions can be exported as interactive Shiny apps as well, I implemented two similar translation functions ComplexHeatmap::heatmap() and ComplexHeatmap::heatmap.2() which use the same set of arguments as the original functions and generate almost identical plots.
Some arguments in the two original functions are ignored in the ComplexHeatmap implementation, but there won’t be any error thrown out.
Then with ComplexHeatmap::heatmap() and ComplexHeatmap::heatmap.2(), you can make your old heatmaps interactive by:
library(ComplexHeatmap)
ht = heatmap(...) # or ht = heatmap.2(...)
library(InteractiveComplexHeatmap)
ht_shiny(ht)
To replace the two original functions with ComplexHeatmap::heatmap() and ComplexHeatmap::heatmap.2(), we need to make sure the new homonymous functions generates the same plots as the old ones. In the following examples, compare_heatmap() basically sends all the arguments to stats::heatmap() and ComplexHeatmap::heatmap() separately and you can directly compare the two plots generated by the two functions.
library(ComplexHeatmap)
stats::heatmap()x = as.matrix(mtcars)
rc = rainbow(nrow(x), start = 0, end = 0.3)
cc = rainbow(ncol(x), start = 0, end = 0.3)
compare_heatmap(x, col = cm.colors(256), scale = "column",
RowSideColors = rc, ColSideColors = cc, margins = c(5,10),
xlab = "specification variables", ylab = "Car Models",
main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
compare_heatmap(x, Colv = NA, col = cm.colors(256), scale = "column",
RowSideColors = rc, margins = c(5,10),
xlab = "specification variables", ylab = "Car Models",
main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
compare_heatmap(x, Rowv = NA, Colv = NA, scale = "column",
main = "heatmap(*, NA, NA) ~= image(t(x))")
Ca = cor(attitude)
cc = rainbow(nrow(Ca))
compare_heatmap(Ca, Rowv = FALSE, RowSideColors = cc, ColSideColors = cc)
cU = cor(USJudgeRatings)
compare_heatmap(cU, Rowv = FALSE, col = topo.colors(16),
distfun = function(c) as.dist(1 - c), keep.dendro = TRUE)
gplots::heatmap.2()Similarly, in the following examples, compare_heatmap.2() basically sends all the arguments to gplots::heatmap.2() and ComplexHeatmap::heatmap.2() separately.
data(mtcars)
x = as.matrix(mtcars)
rc = rainbow(nrow(x), start = 0, end = .3)
cc = rainbow(ncol(x), start = 0, end = .3)
compare_heatmap.2(x)
compare_heatmap.2(x, dendrogram = "none")
compare_heatmap.2(x, dendrogram = "row")
compare_heatmap.2(x, dendrogram = "col")
compare_heatmap.2(x, Rowv = FALSE, dendrogram = "both")
compare_heatmap.2(x, Rowv = NULL, dendrogram = "both")
compare_heatmap.2(x, Colv = FALSE, dendrogram = "both")
compare_heatmap.2(x, reorderfun = function(d, w) reorder(d, w, agglo.FUN = mean))
compare_heatmap.2(x, col = gplots::bluered, scale = "column", tracecol = "#303030")
compare_heatmap.2(x, col = cm.colors(255), scale = "column",
RowSideColors = rc, ColSideColors = cc, margin = c(5, 10),
xlab = "specification variables", ylab = "Car Models",
main = "heatmap(<Mtcars data>, ..., scale=\"column\")",
tracecol = "green", density = "density")
compare_heatmap.2(x, col = cm.colors(255), scale = "column",
RowSideColors = rc, ColSideColors = cc, margin = c(5, 10),
xlab = "specification variables", ylab = "Car Models",
main = "heatmap(<Mtcars data>, ..., scale=\"column\")",
tracecol = "green", density = "density", colRow = rc, colCol = cc,
srtCol = 45, adjCol = c(0.5,1))
data(attitude)
Ca = cor(attitude)
compare_heatmap.2(Ca, symm = TRUE, margin = c(6, 6), trace = "none" )
compare_heatmap.2(Ca, Rowv = FALSE, symm = TRUE, margin = c(6, 6), trace = "none" )
data(USJudgeRatings)
cU = cor(USJudgeRatings)
compare_heatmap.2(cU, Rowv = FALSE, symm = TRUE, col = topo.colors(16),
distfun = function(c) as.dist(1 - c), trace = "none")
hM = format(round(cU, 2))
compare_heatmap.2(cU, Rowv = FALSE, symm = TRUE, col = rev(heat.colors(16)),
distfun = function(c) as.dist(1 - c), trace = "none",
cellnote = hM)
library(affy)
data(SpikeIn)
pms = SpikeIn@pm
compare_heatmap.2(pms, col = rev(heat.colors(16)), main = "SpikeIn@pm",
xlab = "Relative Concentration", ylab = "Probeset",
scale = "row")
data = pms / pms[, "12.50"]
data = ifelse(data > 1, data, -1 / data)
compare_heatmap.2(data, breaks = 16, col = gplots::redgreen, tracecol = "blue",
main = "SpikeIn@pm Fold Changes\nrelative to 12.50 sample",
xlab = "Relative Concentration", ylab = "Probeset")
sessionInfo()
## R version 4.3.3 (2024-02-29)
## Platform: x86_64-apple-darwin20 (64-bit)
## Running under: macOS 26.2
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
##
## locale:
## [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
##
## time zone: Asia/Shanghai
## tzcode source: internal
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] affy_1.78.2 Biobase_2.60.0 BiocGenerics_0.48.1 ComplexHeatmap_2.23.1
## [5] knitr_1.45
##
## loaded via a namespace (and not attached):
## [1] sass_0.4.9 gplots_3.1.3.1 bitops_1.0-7 KernSmooth_2.23-22
## [5] shape_1.4.6.1 gtools_3.9.5 digest_0.6.35 magrittr_2.0.3
## [9] caTools_1.18.2 evaluate_0.23 RColorBrewer_1.1-3 iterators_1.0.14
## [13] circlize_0.4.16 fastmap_1.1.1 foreach_1.5.2 doParallel_1.0.17
## [17] jsonlite_1.8.8 GlobalOptions_0.1.2 BiocManager_1.30.23 preprocessCore_1.62.1
## [21] codetools_0.2-19 jquerylib_0.1.4 cli_3.6.2 rlang_1.1.3
## [25] crayon_1.5.2 cachem_1.0.8 yaml_2.3.8 tools_4.3.3
## [29] parallel_4.3.3 colorspace_2.1-0 GetoptLong_1.0.5 R6_2.5.1
## [33] png_0.1-8 gridGraphics_0.5-1 matrixStats_1.3.0 stats4_4.3.3
## [37] lifecycle_1.0.4 magick_2.8.3 zlibbioc_1.46.0 S4Vectors_0.40.2
## [41] IRanges_2.36.0 clue_0.3-65 cluster_2.1.6 affyio_1.70.0
## [45] bslib_0.7.0 Rcpp_1.0.12 xfun_0.43 highr_0.10
## [49] rjson_0.2.21 htmltools_0.5.8.1 rmarkdown_2.26 Cairo_1.6-2
## [53] compiler_4.3.3